import tkinter as tk
from tkinter import messagebox
from UI.add_windows import on_click_button_task_append, on_click_button_task_edit
from logic.json_function import load_json, save_json
from logic.task_function import del_task
from UI.show_windows import show_info_window

def edit_task(listbox_task, task_information_dict):
    selected_index = listbox_task.curselection()
    if not selected_index:
        messagebox.showwarning("No Task Selected", "Please select a task to edit.")
        return
    
    selected_task_str = listbox_task.get(selected_index)
    task_name, task_priority, task_due_date = selected_task_str.split(' priority:')[0], selected_task_str.split(' priority:')[1].split(' due to ')[0], selected_task_str.split(' due to ')[1]

    for key, value in task_information_dict.items():
        if value['task_name'] == task_name and value['task_priority'] == task_priority and value['task_due_date'] == task_due_date:
            task_key = key
            break
    else:
        messagebox.showerror("Error", "Task not found in the dictionary.")
        return

    on_click_button_task_edit(task_information_dict, listbox_task, task_key, task_name, task_priority, task_due_date)

def window_main():
    window = tk.Tk()
    window.geometry('800x600')
    window.title('To Do List')

    label_text1 = tk.Label(window, text='Task List')
    label_text1.pack()

    # 创建框架来包含 Listbox 和滚动条
    listbox_frame = tk.Frame(window)
    listbox_frame.pack()  

    # 创建 Listbox 和滚动条
    listbox_task = tk.Listbox(listbox_frame, height=10, width=50)
    scrollbar = tk.Scrollbar(listbox_frame, orient=tk.VERTICAL)
    
    # 将 Listbox 和 Scrollbar 关联
    listbox_task.config(yscrollcommand=scrollbar.set)
    scrollbar.config(command=listbox_task.yview)

    # 使用 grid 布局将 Listbox 和 Scrollbar 放在框架内
    listbox_task.pack()
    scrollbar.pack()
    
    try:
        task_dict = load_json('information/task_information.json')
        print(f"Loaded task dictionary: {task_dict}")
    except Exception as e:
        print(f"Error loading task dictionary: {e}")
        task_dict = {}
        
   
    # 加载任务信息到 Listbox
    task_information_dict = load_json('information/task_information.json')
    for each_task in task_information_dict.keys():
        task_name = task_information_dict[each_task]['task_name']
        task_priority = task_information_dict[each_task]['task_priority']
        task_due_date = task_information_dict[each_task]['task_due_date']
        listbox_task.insert(tk.END, f"{task_name} priority:{task_priority} due to {task_due_date}")
    
    listbox = tk.Listbox(listbox_frame)
    listbox.pack()

    # 添加按钮区域
    frame_1 = tk.Frame(window)
    frame_1.pack(pady=20)

    button_task_append = tk.Button(frame_1, text='append a task', command=lambda: on_click_button_task_append(task_information_dict, listbox_task))
    button_task_append.pack(side=tk.LEFT, padx=5)
    
    button_task_del = tk.Button(frame_1, text='delete a task', command=lambda: del_task(listbox_task, task_information_dict))
    button_task_del.pack(side=tk.LEFT, padx=5)
    
    button_task_edit = tk.Button(frame_1, text='edit a task', command=lambda: edit_task(listbox_task, task_information_dict))
    button_task_edit.pack(side=tk.LEFT, padx=5)

    button_show_detail_info = tk.Button(window, text='show detail information', command=lambda: show_info_window(listbox_task, task_information_dict))
    button_show_detail_info.pack()

    window.mainloop()

if __name__ == "__main__":
    window_main()